package com.aries.ui.widget.action.sheet;


import com.aries.ui.widget.BasisDialog;
import com.aries.ui.util.*;
import com.aries.ui.view.alpha.AlphaTextView;
import com.aries.ui.widget.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.utils.TextTool;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.NotNull;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: AriesHoo on 2018/7/19 10:35
 * @E-Mail: AriesHoo@126.com
 * Function: UIActionSheet效果Dialog
 * Description:
 * 1、继承自Dialog 并封装不同Builder模式
 * 2、修改Dialog继承关系并修改默认ListSheetBuilder模式属性设置
 * 3、2018-3-29 13:40:24 新增设置ListBuilder默认imageView默认宽高及文字与图片间距属性控制
 * 修改 getListView及getGridView返回对象错误问题
 * 4、2018-4-3 09:10:18 新增view拖拽关闭交互效果{@link Builder#setDragEnable(boolean)}
 * 5、2018-4-3 12:50:31 将控制虚拟导航栏功能从BasisDialog移至此处
 * 6、2018-4-6 21:43:20 调整设置cancel 及margin逻辑
 * 7、2018-5-23 16:54:27 调整Cancel背景处理逻辑避免因设置GridView 间隔造成问题
 * 8、2018-5-28 16:39:05 修改兼容导航栏问题
 * 9、2018-5-31 16:48:38 删除TranslucentUtil控制导航栏透明度
 * 10、2019-3-11 15:40:52 修改Drawable 设置逻辑避免因Item高度变化适配问题
 * 11、2019-4-11 10:20:43 新增底部导航栏控制相关{ #onAttachedToWindow()}
 * 12、2019-4-18 11:32:04 修改{ GridBuilder.GridAdapter#getView(int, View, ViewGroup)}txt.setLayoutParams 避免5.0以下 崩溃
 * 13、2019-4-22 16:51:54 将view拖拽关闭交互效果移至BasisDialog{@link Builder#setDragEnable(boolean)}
 */
public class UIActionSheetDialog extends BasisDialog<UIActionSheetDialog> {
    private static DependentLayout parentContainer;
    private static ListContainer mGvContainer;
    private static ColorStateListBean tempColorList;
    private static ColorStateListBean ItemTextPosColorList;
    private static ColorStateListBean tempItemTextColorList;
    private static ColorStateListBean tempCancelColorList;
    private static int lastPostion;
    private static boolean mIsCancelable;
    private Context mContext;

    public UIActionSheetDialog(Context context) {
        super(context);
        mContext = context;

    }

    @Override
    protected void onCreate() {
        super.onCreate();
        if (mContentView != null && mContentView.findComponentById(ConstantId.LLAYOUT_ROOTACTIONSHEETDIALOG) != null) {
            mNavigationBottomView = mContentView.findComponentById(ConstantId.LLAYOUT_ROOTACTIONSHEETDIALOG);
        }
    }

    @Override
    protected void onShowing() {
        super.onShowing();
        if (this.isUseAnimation) {
            AnimationUtils.getInstance(mContext).showDialogAnimation(mContentView);
        }
    }

    public UIActionSheetDialog isUseAnimation(boolean isUseAnimation) {
        this.isUseAnimation = isUseAnimation;
        return this;
    }

    /**
     * 获取标题
     *
     * @return 未设置Title文本则返回空
     */
    public Text getTitle() {
        return FindViewUtil.getTargetView(mContentView, ConstantId.TV_TITLEACTIONSHEETDIALOG);
    }

    /**
     * 获取底部取消按钮
     *
     * @return 未设置Cancel文本则返回空
     */
    public Text getCancel() {
        return FindViewUtil.getTargetView(mContentView, ConstantId.TV_CANCELACTIONSHEETDIALOG);
    }

    /**
     * 获取ListView
     *
     * @return 未设置Item或无
     */
    public ListContainer getListView() {
        return FindViewUtil.getTargetView(mContentView, ConstantId.LV_CONTAINERACTIONSHEETDIALOG);
    }

    public ListContainer getGridView() {
        return FindViewUtil.getTargetView(mContentView, ConstantId.GV_CONTAINERACTIONSHEETDIALOG);
    }

//    public GridView getGridView() {
//        return FindViewUtil.getTargetView(mContentView, R.id.gv_containerActionSheetDialog);
//    }

    @Override
    public void dealBackKeyDown() {
        super.dealBackKeyDown();
//        hide();
        if (mIsCancelable) {
            if (isUseAnimation) {
                AnimationUtils.getInstance(mContext).hideDialogAnimation(this);
            } else {
                hide();
            }
        }

    }

    public interface OnItemClickListener {

        /**
         * Item点击事件
         *
         * @param dialog
         * @param itemView
         * @param position
         */
        void onClick(BasisDialog dialog, Component itemView, int position);
    }

    public interface ICreateContentView {
        Component createItemView();

        Builder.SheetAdapter getDefaultAdapter();
    }

    public static class SheetItem {
        CharSequence text;
        ColorStateListBean textColor;
        Element drawable;


        public SheetItem() {
            this("");
        }

        public SheetItem(CharSequence text) {
            this(text, null);
        }

        public SheetItem(CharSequence text, ColorStateListBean color) {
            this(text, color, null);
        }

        public SheetItem(CharSequence text, ColorStateListBean color, Element drawable) {
            this.text = text;
            this.textColor = color;
            this.drawable = drawable;
        }

        public SheetItem setText(CharSequence text) {
            this.text = text;
            return this;
        }


        public SheetItem setImageDrawable(Element drawable) {
            this.drawable = drawable;
            return this;
        }

        public SheetItem setTextColor(ColorStateListBean color) {
            this.textColor = color;
            return this;
        }

        public SheetItem setTextColor(int state, int color) {

            textColor.addStateAndColor(state, color);
            return setTextColor(textColor);
        }
    }

    /**
     * 类微信风格
     */
    public static class ListWeChatBuilder extends ListBuilder<ListWeChatBuilder> {

        public ListWeChatBuilder(Context context) {
            super(context);
            setItemsDividerHeight(0)
                    .setTitleGravity(TextAlignment.LEFT | TextAlignment.VERTICAL_CENTER)
                    .setCancelGravity(TextAlignment.LEFT | TextAlignment.VERTICAL_CENTER)
                    .setItemsTextGravity(TextAlignment.LEFT | TextAlignment.VERTICAL_CENTER)
                    .setItemsGravity(TextAlignment.LEFT | TextAlignment.VERTICAL_CENTER)
                    .setItemsLayoutGravity(TextAlignment.LEFT | TextAlignment.VERTICAL_CENTER)
                    .setCancelMarginTop(0);
        }
    }

    /**
     * 类iOS风格
     */
    public static class ListIOSBuilder extends ListBuilder<ListIOSBuilder> {

        public ListIOSBuilder(Context context) {
            super(context);
            setBackground(null)
//                    .setItemsTopPressedDrawableResourceImage(ResourceTable.Media_action_sheet_top_normal)
//                    .setItemsTopDrawableResourceImage(ResourceTable.Media_action_sheet_top_normal)
//                    .setItemsCenterPressedDrawableResourceImage(ResourceTable.Media_action_sheet_middle_pressed)
//                    .setItemsCenterDrawableResourceImage(ResourceTable.Media_action_sheet_middle_normal)
//                    .setItemsBottomPressedDrawableResourceImage(ResourceTable.Media_action_sheet_bottom_pressed)
//                    .setItemsBottomDrawableResourceImage(ResourceTable.Media_action_sheet_bottom_normal)
//                    .setItemsSinglePressedDrawableResourceImage(ResourceTable.Media_action_sheet_single_pressed)
//                    .setItemsSingleDrawableResourceImage(ResourceTable.Media_action_sheet_single_normal)

                    .setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetItemText)
                    .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetItemText)
                    .setItemsDividerHeight(5)
                    .setPadding(dp2px(8));
        }
    }

    /**
     * 常见风格--类QQ/BottomSheet
     */
    public static class ListSheetBuilder extends ListBuilder<ListSheetBuilder> {
        public ListSheetBuilder(Context context) {
            super(context);

            setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetNormalItemText)
                    .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetNormalItemText);
        }
    }

    private static class ListBuilder<T extends ListBuilder> extends Builder<T> {

        private List<Component> mListHeaderViews;
        private List<Component> mListFooterViews;
        private ListContainer mLvContainer;

        private Element mItemsDivider;
        private int mItemsDividerHeight;
        //标题下方的分割线
        private Component dividerUnderTitle;

        public ListBuilder(Context context) {
            super(context);
            setItemDrawableResource(ResourceTable.Color_colorActionSheetEdge)
                    .setItemPressedDrawableResource(ResourceTable.Color_colorActionSheetEdgePressed)
                    .setItemsDividerResource(ResourceTable.Color_colorActionSheetEdgeLineGray)
                    .setItemsDividerHeightResource(ResourceTable.String_dp_action_sheet_list_line_height)
                    .setItemsTextColorResource(ComponentState.COMPONENT_STATE_PRESSED, ResourceTable.Color_holo_red_dark)
                    .setItemsTextColorResource(BasisDialog.COMPONENT_STATE_LONG_PRESSED, ResourceTable.Color_holo_purple)
                    .setItemsImageWidth(dp2px(26))
                    .setItemsImageHeight(dp2px(26))
                    .setCancelMarginTop(dp2px(8));
        }

        /**
         * { ListView#addHeaderView(View)}
         *
         * @param view view
         * @return backBuilder
         */
        public T addHeaderView(Component view) {
            if (view != null) {
                if (mListHeaderViews == null) {
                    mListHeaderViews = new ArrayList<>();
                }
                mListHeaderViews.add(view);
            }
            return backBuilder();
        }

        /**
         * { ListView#addFooterView(View)}
         *
         * @param view view
         * @return backBuilder
         */
        public T addFooterView(Component view) {
            if (view != null) {
                if (mListFooterViews == null) {
                    mListFooterViews = new ArrayList<>();
                }
                mListFooterViews.add(view);
            }
            return backBuilder();
        }

        public T setItemDrawable(Element drawable) {
            setItemsSingleDrawable(drawable)
                    .setItemsTopDrawable(drawable)
                    .setItemsCenterDrawable(drawable)
                    .setItemsBottomDrawable(drawable);
            return backBuilder();
        }

        public T setItemDrawableResource(int res) {
            return setItemDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemDrawable(shapeElement);
        }

        public T setItemPressedDrawable(Element drawable) {
            setItemsSinglePressedDrawable(drawable).
                    setItemsTopPressedDrawable(drawable)
                    .setItemsCenterPressedDrawable(drawable)
                    .setItemsBottomPressedDrawable(drawable);
            return backBuilder();
        }

        public T setItemPressedDrawableResource(int res) {
            return setItemPressedDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemPressedDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemPressedDrawable(shapeElement);
        }

        public T setItemsTopDrawable(Element drawable) {
            mTopDrawable = drawable;
            return backBuilder();
        }

        public T setItemsTopDrawableResource(int res) {
            return setItemsTopDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsTopDrawableResourceImage(int resImage) {
            return setItemsTopDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsTopDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsTopDrawable(shapeElement);
        }

        public T setItemsTopPressedDrawable(Element drawable) {
            mTopPressedDrawable = drawable;
            return backBuilder();
        }

        public T setItemsTopPressedDrawableResource(int res) {
            return setItemsTopPressedDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsTopPressedDrawableResourceImage(int resImage) {
            return setItemsTopPressedDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsTopPressedDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsTopPressedDrawable(shapeElement);
        }

        public T setItemsCenterDrawable(Element drawable) {
            mCenterDrawable = drawable;
            return backBuilder();
        }

        public T setItemsCenterDrawableResource(int res) {
            return setItemsCenterDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsCenterDrawableResourceImage(int resImage) {
            return setItemsCenterDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsCenterDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsCenterDrawable(shapeElement);
        }

        public T setItemsCenterPressedDrawable(Element drawable) {
            mCenterPressedDrawable = drawable;
            return backBuilder();
        }

        public T setItemsCenterPressedDrawableResource(int res) {
            return setItemsCenterPressedDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsCenterPressedDrawableResourceImage(int resImage) {
            return setItemsCenterPressedDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsCenterPressedDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsCenterPressedDrawable(shapeElement);
        }

        public T setItemsBottomDrawable(Element drawable) {
            mBottomDrawable = drawable;
            return backBuilder();
        }

        public T setItemsBottomDrawableResource(int res) {
            return setItemsBottomDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsBottomDrawableResourceImage(int resImage) {
            return setItemsBottomDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsBottomDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsBottomDrawable(shapeElement);
        }

        public T setItemsBottomPressedDrawable(Element drawable) {
            mBottomPressedDrawable = drawable;
            return backBuilder();
        }

        public T setItemsBottomPressedDrawableResource(int res) {
            return setItemsBottomPressedDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsBottomPressedDrawableResourceImage(int resImage) {
            return setItemsBottomPressedDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsBottomPressedDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsBottomPressedDrawable(shapeElement);
        }

        public T setItemsDivider(Element drawable) {
            mItemsDivider = drawable;
            return backBuilder();
        }

        public T setItemsDividerColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsDivider(shapeElement);
        }

        public T setItemsDividerResource(int res) {
            return setItemsDivider(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsDividerHeight(int height) {
            mItemsDividerHeight = height;
            return backBuilder();
        }

        public T setItemsDividerHeightResource(int res) {
            return setItemsDividerHeight(mResourceUtil.getDimensionPixelSize(res));
        }

        /**
         * 创建标题线框
         *
         * @param isTitle isTitle
         * @return Component
         */
        private Component createLine(boolean isTitle) {
            if (isTitle && (TextUtils.isEmpty(mTitleStr) || mItemsDividerHeight <= 0 || mItemsDivider == null ||
                    ((mListItem == null || mListItem.size() == 0) && (mListHeaderViews == null)))) {
                return null;
            }
            if (!isTitle && (TextUtils.isEmpty(mCancelStr) || mItemsDividerHeight <= 0 || mItemsDivider == null
                    || mCancelMarginTop > 0)) {
                return null;
            }
            Element back = mItemsDivider;
            Component viewLine = new Component(mContext);
            viewLine.setLayoutConfig(new ComponentContainer.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, mItemsDividerHeight));

            mLLayoutRoot.addComponent(viewLine);
            setViewBackground(viewLine, back);
            return viewLine;
        }

        private void createList() {
            if (mListHeaderViews == null && mListFooterViews == null && mListItem == null) {
                return;
            }
            mLvContainer = new ListContainer(mContext);
//            mLvContainer.setLongClickable(false);
            mLvContainer.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT, LayoutAlignment.CENTER, 1.0f));
            mLLayoutRoot.addComponent(mLvContainer);

            mIsCancelable = mCancelable;


            //设置ListView相关
            mLvContainer.setId(ConstantId.LV_CONTAINERACTIONSHEETDIALOG);
            mLvContainer.setLongClickable(false);
//            mLvContainer.setVerticalScrollBarEnabled(false);
//            mLvContainer.setOverScrollMode(View.OVER_SCROLL_NEVER);
            if (mListHeaderViews != null) {

                for (int i = 0; i < mListHeaderViews.size(); i++) {
                    mLvContainer.addComponent(mListHeaderViews.get(i), i);
                }
//                for (Component v : mListHeaderViews) {
//                    mLvContainer.addHeaderView(v);
//                }
            }
            if (mListFooterViews != null) {

                for (int i = 0; i < mListFooterViews.size(); i++) {
                    int footIndex = mListHeaderViews.size() + mAdapter.getCount() - 1;
                    mLvContainer.addComponent(mListFooterViews.get(i), footIndex + i);
                }
            }

//            if (mListFooterViews != null) {
//                for (Component v : mListFooterViews) {
//                    int footIndex = mListHeaderViews.size() + mAdapter.getCount() - 1;
//                    mLvContainer.addFooterView(v);
//                }
//            }
//            mLvContainer.setDivider(DrawableUtil.getNewDrawable(mItemsDivider));
//            mLvContainer.setDividerHeight(mItemsDividerHeight);
            mLvContainer.setItemProvider(mAdapter);
        }

        public void hideDividersOfListAdapter() {
            if (mAdapter != null && mAdapter instanceof ListBuilder.ListAdapter) {
                ((ListAdapter) mAdapter).isHideDividers = true;
                mAdapter.notifyDataChanged();
            }
        }

        public void hideTitleDividersOfListAdapter() {
            dividerUnderTitle.setVisibility(Component.HIDE);
        }

        @Override
        public Component createItemView() {
            dividerUnderTitle = createLine(true);
            createList();
            createLine(false);
            return mLvContainer;
        }

        @Override
        public ListAdapter getDefaultAdapter() {
            return new ListAdapter();
        }

        @NotNull
        private StateElement getBackground(Element mBottomPressedDrawable, Element mBottomDrawable) {
            StateElement background = new StateElement();
            background.addState(new int[]{mStatePressed}, mBottomPressedDrawable);
            background.addState(new int[]{BasisDialog.COMPONENT_STATE_LONG_PRESSED}, mBottomPressedDrawable);
            background.addState(new int[]{mStateEmpty}, mBottomDrawable);
            return background;
        }

        private class ListAdapter extends SheetAdapter {
            //是否隐藏item的分割线
            public boolean isHideDividers = false;

            @Override
            public Component getComponent(final int i, Component convertView, ComponentContainer parent) {
                final SheetItem data = getItem(i);
                final ViewHolder holder;
                System.out.println("getComponent   + =====");


                StateElement background = null;
                if (convertView == null) {
                    convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_action_sheet_list, null, false);
                    holder = new ViewHolder();


                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

//                convertView.setLongClickable(true);

                holder.imageView = (Image) convertView.findComponentById(ResourceTable.Id_iv_iconActionSheetList);
                holder.textView = (AlphaTextView) convertView.findComponentById(ResourceTable.Id_tv_msgActionSheetList);
                holder.divider = convertView.findComponentById(ResourceTable.Id_divider);
                holder.item = (DirectionalLayout) convertView.findComponentById(ResourceTable.Id_item);

                if (data.drawable != null) {
                    DrawableUtil.setDrawableWidthHeight(data.drawable, mItemsImageWidth, mItemsImageHeight);
                    holder.imageView.setVisibility(Component.VISIBLE);
                    holder.imageView.setImageElement(data.drawable);
                    holder.imageView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                    holder.imageView.setClipAlignment(LayoutAlignment.CENTER);
                    ComponentContainer.LayoutConfig params = (ComponentContainer.LayoutConfig) holder.imageView.getLayoutConfig();
                    params.height = mItemsImageHeight;
                    params.width = mItemsImageWidth;
                    params.setMarginRight(mTextDrawablePadding);
                    holder.imageView.setLayoutConfig(params);

                } else {
                    holder.imageView.setVisibility(Component.HIDE);
                }

                data.setTextColor(mItemsTextColor);

                setTextView(holder.textView, data, i);

                //设置分割线
                if (mItemsDividerHeight <= 0 || isHideDividers) {
                    holder.divider.setVisibility(Component.HIDE);
                } else {
                    if (i == getCount() - 1) {
                        holder.divider.setVisibility(Component.HIDE);
                    } else {
                        holder.divider.setVisibility(Component.VISIBLE);
                        holder.divider.setBackground(DrawableUtil.getNewDrawable(mItemsDivider));
                        ComponentContainer.LayoutConfig layoutConfig = holder.divider.getLayoutConfig();
                        layoutConfig.height = mItemsDividerHeight;
                        holder.divider.setLayoutConfig(layoutConfig);
                    }
                }
                holder.item.setAlignment(mItemsLayoutGravity);
                holder.item.setMinHeight(mItemsMinHeight);
                holder.item.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);

                int size = getCount();
                int sizeHeader = mListHeaderViews != null ? mListHeaderViews.size() : 0;
                boolean hasTitle = !TextUtils.isEmpty(mTitleStr);
                boolean hasMargin = mCancelMarginTop > 0 || TextUtils.isEmpty(mCancelStr);
                if (size == 1) {
                    if (hasTitle || sizeHeader > 0) {
                        StateElement center = getBackground(mCenterPressedDrawable, mCenterDrawable);
                        StateElement bottom = getBackground(mBottomPressedDrawable, mBottomDrawable);
                        background = hasMargin ? bottom : center;
                    } else {
                        StateElement single = getBackground(mSinglePressedDrawable, mSingleDrawable);
                        StateElement top = getBackground(mCenterPressedDrawable, mCenterDrawable);
                        background = hasMargin ? single : top;
                    }
                } else {
                    if (hasTitle || sizeHeader > 0) {
                        if (i >= 0 && i < size - 1) {
                            background = getBackground(mCenterPressedDrawable, mCenterDrawable);
//
                        } else {
                            StateElement center = getBackground(mCenterPressedDrawable, mCenterDrawable);

                            StateElement bottom = getBackground(mBottomPressedDrawable, mBottomDrawable);

                            background = hasMargin ? bottom : center;
                        }
                    } else {
                        if (i == 0) {
                            background = getBackground(mTopPressedDrawable, mTopDrawable);
                        } else if (i < size - 1) {
                            background = getBackground(mCenterPressedDrawable, mCenterDrawable);
                        } else {
                            StateElement center = getBackground(mCenterPressedDrawable, mCenterDrawable);
                            StateElement bottom = getBackground(mBottomPressedDrawable, mBottomDrawable);
                            background = hasMargin ? bottom : center;
                        }
                    }
                }
//                background = background;
                setViewBackground(convertView, background);
                convertView.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (mItemsClickDismissEnable) {
//                            mDialog.hide();
                            if (mDialog.isUseAnimation) {
                                AnimationUtils.getInstance(mContext).hideDialogAnimation(mDialog);
                            } else {
                                mDialog.hide();
                            }

                        }
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onClick(mDialog, component, i);
                        }
                    }
                });
                return convertView;
            }


        }
    }

    /**
     * 网格布局
     */
    public static class GridBuilder extends Builder<GridBuilder> {

        private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "GridBuilder.class");
        private Element mGridBackground;
        private int mGridPadding;
//        private int mNumColumns = GridView.AUTO_FIT;
//        private int mStretchMode = GridView.STRETCH_COLUMN_WIDTH;

        private int mNumColumns = 0;
        private int mStretchMode = 0;
        private int mHorizontalSpacing;
        private int mVerticalSpacing;
        private boolean mItemsClickBackgroundEnable;

        public static HiLogLabel getLabelLog() {
            return LABEL_LOG;
        }

        public int getmStretchMode() {
            return mStretchMode;
        }

        public int getmHorizontalSpacing() {
            return mHorizontalSpacing;
        }

        public int getmVerticalSpacing() {
            return mVerticalSpacing;
        }

        public GridBuilder(Context context) {
            super(context);
            setNumColumns(3)

                    .setGridBackgroundColor(Color.WHITE.getValue())
                    .setGridPadding(dp2px(12))
                    .setItemsTextSize(12)
                    .setItemsSingleDrawableResource(ResourceTable.Color_colorActionSheetEdge)
                    .setItemsSinglePressedDrawableResource(ResourceTable.Color_colorActionSheetEdgePressed);
        }

        /**
         * 设置Grid 背景drawable
         *
         * @param drawable drawable
         * @return GridBuilder
         */
        public GridBuilder setGridBackground(Element drawable) {
            mGridBackground = drawable;
            return this;
        }

        /**
         * 设置Grid背景颜色
         *
         * @param color color
         * @return GridBuilder
         */
        public GridBuilder setGridBackgroundColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setGridBackground(shapeElement);
        }

        /**
         * 设置Grid 背景资源
         *
         * @param res res
         * @return GridBuilder
         */
        public GridBuilder setGridBackgroundResource(int res) {
            return setGridBackground(mResourceUtil.getColorDrawable(res));
        }

        /**
         * 设置是否item点击/按下整个item背景效果--默认为透明度变化
         *
         * @param enable enable
         * @return GridBuilder
         */
        public GridBuilder setItemsClickBackgroundEnable(boolean enable) {
            this.mItemsClickBackgroundEnable = enable;
            return this;
        }

        /**
         * 设置Grid padding
         *
         * @param padding padding
         * @return GridBuilder
         */
        public GridBuilder setGridPadding(int padding) {
            mGridPadding = padding;
            return this;
        }

        /**
         * { GridView#setHorizontalSpacing(int)}
         *
         * @param spacing spacing
         * @return GridBuilder
         */
        public GridBuilder setHorizontalSpacing(int spacing) {
            this.mHorizontalSpacing = spacing;
            return this;
        }

        /**
         * { GridView#setVerticalSpacing(int)}
         *
         * @param spacing spacing
         * @return GridBuilder
         */
        public GridBuilder setVerticalSpacing(int spacing) {
            this.mVerticalSpacing = spacing;
            return this;
        }

        /**
         * { GridView#setNumColumns(int)} (int)}
         *
         * @param numColumns numColumns
         * @return GridBuilder
         */
        public GridBuilder setNumColumns(int numColumns) {
            this.mNumColumns = numColumns;
            return this;
        }

        /**
         * { GridView#setStretchMode(int)}
         *
         * @param mode mode
         * @return GridBuilder
         */
        public GridBuilder setStretchMode(int mode) {
            this.mStretchMode = mode;
            return this;
        }

        @Override
        public Component createItemView() {
            mGvContainer = new ListContainer(mContext);
            mGvContainer.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT, LayoutAlignment.CENTER, 1.0f));


            mLLayoutRoot.addComponent(mGvContainer);

            //设置GridView相关属性
            mGvContainer.setId(ConstantId.GV_CONTAINERACTIONSHEETDIALOG);
//            mGvContainer.setVerticalScrollBarEnabled(false);
//            mGvContainer.setOverScrollMode(View.OVER_SCROLL_NEVER);
//            mGvContainer.setNumColumns(mNumColumns);
//            mGvContainer.setStretchMode(mStretchMode);
//            mGvContainer.setHorizontalSpacing(mHorizontalSpacing);
//            mGvContainer.setVerticalSpacing(mVerticalSpacing);
            mGvContainer.setItemProvider(mAdapter);
            mGvContainer.setPadding(mGridPadding, mGridPadding, mGridPadding, mGridPadding);
            mGvContainer.setLongClickable(false);
            setViewBackground(mGvContainer, mGridBackground);
            setViewBackground(mTvTitle, mGridBackground);
//            if (mItemsClickBackgroundEnable) {
//                mGvContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        if (mItemsClickDismissEnable) {
//                            mDialog.dismiss();
//                        }
//                        if (mOnItemClickListener != null) {
//                            mOnItemClickListener.onClick(mDialog, view, position);
//                        }
//                    }
//                });
//            }
            return mGvContainer;
        }

        @Override
        public GridAdapter getDefaultAdapter() {
            return new GridAdapter();
        }

        private class GridAdapter extends SheetAdapter {


            @Override
            public int getCount() {
                if (mListItem != null && mListItem.size() > 0) {
                    int i = mListItem.size() % mNumColumns;
                    if (i > 0) {
                        return mListItem.size() / mNumColumns + i;
                    } else {
                        return mListItem.size() / mNumColumns;
                    }
                }
                return 0;
            }

            @Override
            public SheetItem getItem(int position) {

                return null;
            }

            @Override
            public long getItemId(int position) {
                return super.getItemId(position);
            }

            @Override
            public Component getComponent(final int position, Component convertView, ComponentContainer parent) {
                int realPos = 0;

                ViewHolder holder;

                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_action_sheet_grid, null, false);
                    holder.item = (DirectionalLayout) convertView.findComponentById(ResourceTable.Id_item_container);
                    holder.item.setTotalWeight(mNumColumns);
//           convertView = txt;

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                holder.item.removeAllComponents();
                for (int j = position * mNumColumns; j < mListItem.size(); j++) {
                    if (j >= position * mNumColumns && j < (position + 1) * mNumColumns) {
                        AlphaTextView textView = new AlphaTextView(mContext);
                        textView.setId(ConstantId.GV_ITEM + (j));

                        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
                        layoutConfig.weight = 1;

                        textView.setLayoutConfig(layoutConfig);

                        holder.item.addComponent(textView);

//                        realPos = position * mNumColumns + childIndex;
                        realPos = j;
                        final SheetItem data = mListItem.get(realPos);

                        data.setTextColor(mItemsTextColor);

                        setTextView(textView, data, realPos);
                        DrawableUtil.setDrawableWidthHeight(data.drawable, mItemsImageWidth, mItemsImageHeight);
                        textView.setAroundElements(null, data.drawable, null, null);
                        textView.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
                        if (!mItemsClickBackgroundEnable) {
                            int finalRealPos = realPos;
                            textView.setClickedListener(new Component.ClickedListener() {
                                @Override
                                public void onClick(Component component) {
                                    if (mItemsClickDismissEnable) {
                                        mDialog.hide();
                                    }
                                    if (mOnItemClickListener != null) {
                                        mOnItemClickListener.onClick(mDialog, component, finalRealPos);
                                    }
                                }
                            });
                        }
                    }
                }


//                for (int i = position * mNumColumns; i < mListItem.size(); i++) {
//                    if (i >= position * mNumColumns && i < (position + 1) * mNumColumns) {
//                        AlphaTextView textView = (AlphaTextView) convertView.findComponentById(ConstantId.GV_ITEM + (i));
//
//                        int childIndex = holder.item.getChildIndex(textView);
//
//                        realPos = position * mNumColumns + childIndex;
//                        final SheetItem data = mListItem.get(realPos);
//
//                        data.setTextColor(mItemsTextColor);
//
//                        setTextView(textView, data, realPos);
//                        DrawableUtil.setDrawableWidthHeight(data.drawable, mItemsImageWidth, mItemsImageHeight);
//                        textView.setLongClickable(false);
//                        textView.setAroundElements(null, data.drawable, null, null);
//                        textView.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
//                        if (!mItemsClickBackgroundEnable) {
//                            int finalRealPos = realPos;
//                            textView.setClickedListener(new Component.ClickedListener() {
//                                @Override
//                                public void onClick(Component component) {
//                                    if (mItemsClickDismissEnable) {
//                                        mDialog.hide();
//                                    }
//                                    if (mOnItemClickListener != null) {
//                                        mOnItemClickListener.onClick(mDialog, component, finalRealPos);
//                                    }
//                                }
//                            });
//                        }
//                    }
//                }


                return convertView;
            }
        }
    }

    private static abstract class Builder<T extends Builder> extends BasisBuilder<T> implements ICreateContentView {

//        protected StateListDrawable mStateDrawableTop;
//        protected StateListDrawable mStateDrawableCenter;
//        protected StateListDrawable mStateDrawableBottom;
//        protected StateListDrawable mStateDrawableSingle;

        protected StateElement mStateDrawableTop;
        protected StateElement mStateDrawableCenter;
        protected StateElement mStateDrawableBottom;
        protected StateElement mStateDrawableSingle;

        protected Element mTopDrawable;
        protected Element mTopPressedDrawable;
        protected Element mCenterDrawable;
        protected Element mCenterPressedDrawable;
        protected Element mBottomDrawable;
        protected Element mBottomPressedDrawable;
        protected Element mSingleDrawable;
        protected Element mSinglePressedDrawable;

        protected int mMarginTop;
        protected List<SheetItem> mListItem;

        protected Text mTvTitle;
        protected CharSequence mTitleStr;
        protected ColorStateListBean mTitleTextColor;
        protected int mTitleGravity = TextAlignment.CENTER;
        protected float mTitleTextSize = 16;

        protected Text mTvCancel;
        protected CharSequence mCancelStr;
        protected ColorStateListBean mCancelTextColor;
        protected int mCancelGravity = TextAlignment.CENTER;
        protected float mCancelTextSize = 16;
        protected int mCancelMarginTop;

        protected Map<Integer, ColorStateListBean> mMapItemColor;
        protected SheetAdapter mAdapter;

        protected OnItemClickListener mOnItemClickListener;

        protected int mTextDrawablePadding;
        protected float mItemsTextSize = 16;
        protected int mItemsMinHeight = 45;
        protected ColorStateListBean mItemsTextColor;
        protected int mItemsGravity = TextAlignment.CENTER;
        protected int mItemsLayoutGravity = LayoutAlignment.CENTER;
        protected int mItemsTextGravity = TextAlignment.CENTER;
        protected int mItemsImageWidth = -1;
        protected int mItemsImageHeight = -1;
        protected int mItemsTextPaddingLeft;
        protected int mItemsTextPaddingTop;
        protected int mItemsTextPaddingRight;
        protected int mItemsTextPaddingBottom;
        protected boolean mItemsClickDismissEnable = true;
        Component mViewItem;
        private int mGravity = LayoutAlignment.BOTTOM;

        public Builder(Context context) {
            super(context);
//            Log.i("height", "height:" + getScreenHeight());
            setBackgroundResource(ResourceTable.Color_colorActionSheetNormalBackground)
                    .setItemsSingleDrawableResource(ResourceTable.Color_colorActionSheetEdge)
                    .setItemsSinglePressedDrawableResource(ResourceTable.Color_colorActionSheetEdgePressed)
                    .setMarginTop((int) (getScreenHeight() * 0.3))
                    .setTextPadding(dp2px(16), dp2px(10), dp2px(16), dp2px(10))
                    .setTitleTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetTitleText)
                    .setCancelTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetWeiXinText)
                    .setItemsTextColorResource(ComponentState.COMPONENT_STATE_EMPTY, ResourceTable.Color_colorActionSheetWeiXinText)
                    //设置 按压颜色
                    .setTitleTextColorResource(BasisDialog.COMPONENT_STATE_LONG_PRESSED, ResourceTable.Color_colorActionSheetCancelText)
                    .setCancelTextColorResource(BasisDialog.COMPONENT_STATE_LONG_PRESSED, ResourceTable.Color_holo_red_dark)
                    .setItemsTextColorResource(BasisDialog.COMPONENT_STATE_LONG_PRESSED, ResourceTable.Color_holo_purple)
                    .setItemsTextColorResource(ComponentState.COMPONENT_STATE_PRESSED, ResourceTable.Color_holo_purple)
                    .setItemsMinHeight((int) dp2px(45))
                    .setTextDrawablePadding((int) dp2px(12))
                    .setPadding(0);
        }

        /**
         * 设置ContentView距离顶部间距
         *
         * @param top top
         * @return 设置ContentView
         */
        public T setMarginTop(int top) {
            mMarginTop = top;
            return backBuilder();
        }

        /**
         * 设置文本与img 间距
         *
         * @param padding 间距
         * @return backBuilder
         */
        public T setTextDrawablePadding(int padding) {
            this.mTextDrawablePadding = padding;
            return backBuilder();
        }

        /**
         * 设置Item、标题、取消文本间距
         *
         * @param left left
         * @param top top
         * @param right right
         * @param bottom bottom
         * @return backBuilder
         */
        public T setTextPadding(float left, float top, float right, float bottom) {
            this.mItemsTextPaddingLeft = (int) left;
            this.mItemsTextPaddingTop = (int) top;
            this.mItemsTextPaddingRight = (int) right;
            this.mItemsTextPaddingBottom = (int) bottom;
            return backBuilder();
        }

        /**
         * 设置标题
         * { TextView#setText(CharSequence)}
         *
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setTitle(CharSequence charSequence) {
            this.mTitleStr = charSequence;
            return backBuilder();
        }

        public T setTitle(int resId) {
            return setTitle(mResourceUtil.getText(resId));
        }

        /**
         * 设置标题文字颜色
         * { TextView#setTextColor(ColorStateList)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setTitleTextColor(ColorStateListBean color) {
            this.mTitleTextColor = color;
            return backBuilder();
        }

        /**
         * 设置标题文字颜色
         * { TextView#setTextColor(ColorStateList)}
         *
         * @param color 文字颜色
         * @param state state
         * @return 设置标题文字颜色
         */
        public T setTitleTextColor(int state, int color) {
            if (tempColorList == null) {
                tempColorList = new ColorStateListBean();
            }

            tempColorList.addStateAndColor(state, color);

            return setTitleTextColor(tempColorList);
        }

        /**
         * 设置标题文字颜色
         * { TextView#setTextColor(ColorStateList)}
         *
         * @param resId resId
         * @param state state
         * @return 文字颜色
         */
        public T setTitleTextColorResource(int state, int resId) {

            if (tempColorList == null) {
                tempColorList = new ColorStateListBean();
            }

            tempColorList.addStateAndColor(state, mContext.getColor(resId));

            return setTitleTextColor(tempColorList);
//            return setTitleTextColor(mResourceUtil.getColorStateList(resId));
        }

        /**
         * 设置标题文本尺寸
         * { TextView#setTextSize(int, float)}
         * { Builder#setTextSizeUnit(int)}
         *
         * @param size 文本尺寸
         * @return backBuilder
         */
        public T setTitleTextSize(float size) {
            this.mTitleTextSize = size;
            return backBuilder();
        }

        /**
         * 设置标题文本对齐方式
         * { TextView#setGravity(int)}
         *
         * @param gravity 本对齐方式
         * @return backBuilder
         */
        public T setTitleGravity(int gravity) {
            this.mTitleGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置取消按钮文本
         *
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setCancel(CharSequence charSequence) {
            this.mCancelStr = charSequence;
            return backBuilder();
        }

        public T setCancel(int resId) {
            return setCancel(mResourceUtil.getText(resId));
        }

        /**
         * 设置取消文本颜色
         *
         * @param color 文本颜色
         * @return backBuilder
         */
        public T setCancelTextColor(ColorStateListBean color) {
            this.mCancelTextColor = color;
            return backBuilder();
        }

        public T setCancelTextColor(int state, int color) {
            if (tempCancelColorList == null) {
                tempCancelColorList = new ColorStateListBean();
            }
            tempCancelColorList.addStateAndColor(state, color);
            return setCancelTextColor(tempCancelColorList);
        }

        public T setCancelTextColorResource(int state, int resId) {
            if (tempCancelColorList == null) {
                tempCancelColorList = new ColorStateListBean();
            }
            tempCancelColorList.addStateAndColor(state, mContext.getColor(resId));
            return setCancelTextColor(tempCancelColorList);
        }

        /**
         * 设置取消文本尺寸
         *
         * @param size size
         * @return 文本尺寸
         */
        public T setCancelTextSize(float size) {
            this.mCancelTextSize = size;
            return backBuilder();
        }

        /**
         * 设置取消文本对齐方式
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setCancelGravity(int gravity) {
            this.mCancelGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置取消文本与Item间距
         *
         * @param margin 间距
         * @return backBuilder
         */
        public T setCancelMarginTop(int margin) {
            this.mCancelMarginTop = margin;
            return backBuilder();
        }

        /**
         * 设置Item 适配器
         *
         * @param adapter 适配器
         * @return backBuilder
         */
        public T setAdapter(ListBuilder.ListAdapter adapter) {
            this.mAdapter = adapter;
            return backBuilder();
        }

        /**
         * 设置单个Item默认状态背景Drawable
         *
         * @param drawable 背景Drawable
         * @return backBuilder
         */
        public T setItemsSingleDrawable(Element drawable) {
            mSingleDrawable = drawable;
            return backBuilder();
        }

        public T setItemsSingleDrawableResource(int res) {
            return setItemsSingleDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsSingleDrawableResourceImage(int resImage) {
            return setItemsSingleDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsSingleDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsSingleDrawable(shapeElement);
        }

        /**
         * 设置单个Item按下状态背景Drawable
         *
         * @param drawable 背景Drawable
         * @return backBuilder
         */
        public T setItemsSinglePressedDrawable(Element drawable) {
            mSinglePressedDrawable = drawable;
            return backBuilder();
        }

        public T setItemsSinglePressedDrawableResource(int res) {
            return setItemsSinglePressedDrawable(mResourceUtil.getColorDrawable(res));
        }

        public T setItemsSinglePressedDrawableResourceImage(int resImage) {
            return setItemsSinglePressedDrawable(mResourceUtil.getImageDrawable(resImage));
        }

        public T setItemsSinglePressedDrawableColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setItemsSinglePressedDrawable(shapeElement);
        }

        /**
         * 设置Item的最小高度
         *
         * @param height 小高度
         * @return backBuilder
         */
        public T setItemsMinHeight(int height) {
            this.mItemsMinHeight = height;
            return backBuilder();
        }

        /**
         * 设置item 文本颜色-设置全部文本默认颜色
         * 设置单个文本颜色参考
         * { Builder#setItemTextColor(int, ColorStateList)}
         * { Builder#setItemTextColor(int, int)}
         * { Builder#setItemTextColorResource(int, int)}
         *
         * @param color 颜色参考
         * @return backBuilder
         */
        public T setItemsTextColor(ColorStateListBean color) {

            this.mItemsTextColor = color;
            return backBuilder();
        }

        public T setItemsTextColor(int state, int color) {
            if (tempItemTextColorList == null) {
                tempItemTextColorList = new ColorStateListBean();
            }
            tempItemTextColorList.addStateAndColor(state, color);
            return setItemsTextColor(tempItemTextColorList);
        }

        public T setItemsTextColorResource(int state, int resId) {
            if (tempItemTextColorList == null) {
                tempItemTextColorList = new ColorStateListBean();
            }
            tempItemTextColorList.addStateAndColor(state, mContext.getColor(resId));
            return setItemsTextColor(tempItemTextColorList);
        }

        /**
         * 设置某个item文本颜色
         *
         * @param position position
         * @param color 文本颜色
         * @return backBuilder
         */
        public T setItemTextColor(int position, ColorStateListBean color) {
            if (mMapItemColor == null || position != lastPostion) {
                mMapItemColor = new HashMap<>();
            }
            mMapItemColor.put(position, color);
            lastPostion = position;
            return backBuilder();
        }

        public T setItemTextColor(int position, int state, int color) {
            if (ItemTextPosColorList == null || position != lastPostion) {
                ItemTextPosColorList = new ColorStateListBean();
            }
            ItemTextPosColorList.addStateAndColor(state, color);
            lastPostion = position;
            return setItemTextColor(position, ItemTextPosColorList);
        }

        public T setItemTextColorResource(int position, int state, int resId) {

            if (ItemTextPosColorList == null || position != lastPostion) {
                ItemTextPosColorList = new ColorStateListBean();
            }
            ItemTextPosColorList.addStateAndColor(state, mContext.getColor(resId));
            lastPostion = position;
            return setItemTextColor(position, ItemTextPosColorList);
        }

        /**
         * 设置所有item文本尺寸
         *
         * @param size 文本尺寸
         * @return backBuilder
         */
        public T setItemsTextSize(float size) {
            this.mItemsTextSize = size;
            return backBuilder();
        }

        /**
         * 设置所有Item 对齐方式
         * { LinearLayout#setGravity(int)}
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setItemsGravity(int gravity) {
            this.mItemsGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置所有Item 对齐方式
         * { LinearLayout#setGravity(int)}
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setItemsLayoutGravity(int gravity) {
            this.mItemsLayoutGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置所有Item对齐方式
         * { TextView#setGravity(int)}
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setItemsTextGravity(int gravity) {
            this.mItemsTextGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置Item 图标宽度默认原始宽度
         *
         * @param width width
         * @return backBuilder
         */
        public T setItemsImageWidth(int width) {
            mItemsImageWidth = width;
            return backBuilder();
        }

        /**
         * 设置Item图标高度默认原始高度
         *
         * @param height height
         * @return backBuilder
         */
        public T setItemsImageHeight(int height) {
            mItemsImageHeight = height;
            return backBuilder();
        }

        /**
         * 设置点击Item是否dialog关闭
         *
         * @param enable 是否dialog关闭
         * @return backBuilder
         */
        public T setItemsClickDismissEnable(boolean enable) {
            this.mItemsClickDismissEnable = enable;
            return backBuilder();
        }

        /**
         * 添加一个Item
         *
         * @param item item
         * @return backBuilder
         */
        public T addItem(SheetItem item) {
            if (item != null) {
                if (mListItem == null) {
                    mListItem = new ArrayList<>();
                }
                mListItem.add(item);
            }
            return backBuilder();
        }

        /**
         * 添加一个纯文本item
         *
         * @param txt txt
         * @return 纯文本item
         */
        public T addItem(CharSequence txt) {
            return addItem(new SheetItem(txt));
        }

        public T addItem(int txt) {
            return addItem(new SheetItem(mResourceUtil.getText(txt)));
        }

        public T addItem(int txt, int resDrawable) {
            return addItem(new SheetItem(mResourceUtil.getText(txt))
                    .setImageDrawable(mResourceUtil.getImageDrawable(resDrawable)));
        }

        public T addItem(CharSequence txt, int resDrawable) {
            return addItem(new SheetItem(txt).setImageDrawable(mResourceUtil.getImageDrawable(resDrawable)));
        }

        /**
         * 添加item数组
         *
         * @param list item数组
         * @return backBuilder
         */
        public T addItems(ArrayList<SheetItem> list) {
            if (list != null && list.size() > 0) {
                if (mListItem == null) {
                    mListItem = new ArrayList<>();
                }
                mListItem.addAll(list);
            }
            return backBuilder();
        }

        /**
         * 添加纯文本Item list数组
         *
         * @param items list数组
         * @return backBuilder
         */
        public T addItems(List<CharSequence> items) {
            if (items != null && items.size() > 0) {
                if (mListItem == null) {
                    mListItem = new ArrayList<>();
                }
                for (CharSequence item : items) {
                    addItem(item);
                }
            }
            return backBuilder();
        }

        /**
         * 添加纯文本Item []数组
         *
         * @param items []数组
         * @return 文本Item
         */
        public T addItems(CharSequence[] items) {
            return addItems(Arrays.asList(items));
        }

        /**
         * 添加一个array资源item
         *
         * @param itemsRes 资源item
         * @return 资源item
         */
        public T addItems(int itemsRes) {
            return addItems(mResourceUtil.getTextArray(itemsRes));
        }

        /**
         * 设置item点击事件监听
         *
         * @param listener 点击事件监听
         * @return backBuilder
         */
        public T setOnItemClickListener(OnItemClickListener listener) {
            this.mOnItemClickListener = listener;
            return backBuilder();
        }

        /**
         * backBuilder
         *
         * @param gravity gravity
         * @return backBuilder
         */
        public T setGravity(int gravity) {
            mGravity = gravity;
            return backBuilder();
        }

        /**
         * 创建dialog
         *
         * @return UIActionSheetDialog
         */
        public UIActionSheetDialog create() {
            mDialog = new UIActionSheetDialog(mContext);
            mDialog.setType(BasisDialog.UI_ACTION);

            setDialog();
            parentContainer = new DependentLayout(mContext);

            parentContainer.removeAllComponents();

            DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
//            parentContainer.setOrientation(DirectionalLayout.VERTICAL);
            parentContainer.setLayoutConfig(layoutConfig);
            mDialog.setGravity(mGravity);
            parentContainer.setAlignment(mGravity);
            Component contentView = createContentView();
            parentContainer.addComponent(contentView);
            mDialog.setContentView(parentContainer);

            mDialog.setTransparent(true);

            WindowManager.LayoutConfig config = new WindowManager.LayoutConfig();
            config.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
            config.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            mDialog.setAttributes(config);


//            mDialog.setAlignment(mDialog.mGravity);
//            mDialog.setSize(mDialog.getWindow().getLayoutConfig().get().width,mDialog.getWindow().getLayoutConfig().get().height);
//            mDialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, contentView.getEstimatedHeight());
            mDialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            mDialog.setMargin(0, mMarginTop, 0, 0);
            return (UIActionSheetDialog) mDialog;
        }

        /**
         * 创建dialog ContentView
         *
         * @return  Component
         */
        private Component createContentView() {
            createDrawable();
            mLLayoutRoot = new DirectionalLayout(mContext);
            mLLayoutRoot.removeAllComponents();
            mLLayoutRoot.setId(ConstantId.LLAYOUT_ROOTACTIONSHEETDIALOG);
            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            mLLayoutRoot.setOrientation(DirectionalLayout.VERTICAL);
            mLLayoutRoot.setLayoutConfig(layoutConfig);


//            parentContainer.removeAllComponents();
//            parentContainer.setPadding(mPadding, mPadding, mPadding, mPadding);

            mAdapter = mAdapter == null ? getDefaultAdapter() : mAdapter;
            setLayoutRootView();

            createTitle();
            mViewItem = createItemView();
            createCancel();
            return mLLayoutRoot;
        }

        protected void setLayoutRootView() {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                mLLayoutRoot.setElevation(mElevation);
//            }
            mLLayoutRoot.removeAllComponents();
//            mLLayoutRoot.setPadding(mPadding, mPadding, mPadding, mPadding);
            mLLayoutRoot.setMarginLeft(mPadding);
            mLLayoutRoot.setMarginRight(mPadding);
            mLLayoutRoot.setMarginTop(mPadding);
            mLLayoutRoot.setMarginBottom(mPadding);

            mBackground = getBackground();
            if (mBackground != null) {
                setViewBackground(mLLayoutRoot, mBackground);
            }
        }

        /**
         * 创建Title
         */
        private void createTitle() {
            if (TextTool.isNullOrEmpty(mTitleStr)) {
                return;
            }
            mTvTitle = new Text(mContext);
            mTvTitle.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
            mTvTitle.setMinHeight(dp2px(20));
            mTvTitle.setId(ConstantId.TV_TITLEACTIONSHEETDIALOG);
            mLLayoutRoot.addComponent(mTvTitle);

            Element background = mStateDrawableSingle;
            mTvTitle.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
            mTvTitle.setTextAlignment(mTitleGravity);
            mTvTitle.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
//            mTvTitle.setCompoundDrawablePadding(mTextDrawablePadding);
            mTvTitle.setAroundElementsPadding(mTextDrawablePadding);
//            mTvTitle.setText(mTitleStr.toString());
            mTvTitle.setText(mTitleStr.toString());
            mTvTitle.setTextSize((int) mTitleTextSize, mTextSizeUnit);


            mTvTitle.setTextColor(new Color(mTitleTextColor.getColor(ComponentState.COMPONENT_STATE_EMPTY)));


            boolean hasList = mListItem != null && mListItem.size() > 0;
            boolean hasCancel = !TextUtils.isEmpty(mCancelStr) && mCancelMarginTop <= 0;
            if (hasList || hasCancel) {
                background = mStateDrawableTop;
            }

            mTvTitle.setComponentStateChangedListener((component, state) -> {
//                        if (ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)){
                if (state == BasisDialog.COMPONENT_STATE_LONG_PRESSED || ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)) {
                    mTvTitle.setTextColor(mTitleTextColor.getColor(state) == 0 ? mTvTitle.getTextColor() : new Color(mTitleTextColor.getColor(state)));

                } else {
                    mTvTitle.setTextColor(mTitleTextColor.getColor(state) == 0 ? mTvTitle.getTextColor() : new Color(mTitleTextColor.getColor(ComponentState.COMPONENT_STATE_EMPTY)));
                }

            });

            setViewBackground(mTvTitle, background);
            setTextViewLine(mTvTitle);
        }

        /**
         * 创建取消按钮
         */
        private void createCancel() {

            if (TextTool.isNullOrEmpty(mCancelStr)) {
                return;
            }

            mTvCancel = new Text(mContext);
            mTvCancel.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
            mTvCancel.setMinHeight(mItemsMinHeight);
            mTvCancel.setId(ConstantId.TV_CANCELACTIONSHEETDIALOG);
            mLLayoutRoot.addComponent(mTvCancel);

            mTvCancel.setLineSpacing(mLineSpacingExtra, mLineSpacingMultiplier);
            mTvCancel.setTextAlignment(mCancelGravity);
//            mTvCancel.setCompoundDrawablePadding(mTextDrawablePadding);
            mTvCancel.setAroundElementsPadding(mTextDrawablePadding);
            mTvCancel.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
            mTvCancel.setText(mCancelStr.toString());
            mTvCancel.setTextSize((int) mCancelTextSize, mTextSizeUnit);
            mTvCancel.setTextColor(new Color(mCancelTextColor.getColor(ComponentState.COMPONENT_STATE_EMPTY)));
            if (mCancelMarginTop > 0) {
                ComponentContainer.LayoutConfig lp = mTvCancel.getLayoutConfig();
                if (lp != null) {
//                    lp.topMargin = mCancelMarginTop;
                    lp.setMarginTop(mCancelMarginTop);
                    mTvCancel.setLayoutConfig(lp);
                }
            }

            mTvCancel.setComponentStateChangedListener((component, state) -> {
//                        if (ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)){
                if (state == BasisDialog.COMPONENT_STATE_LONG_PRESSED || ComponentState.isStateMatched(state, ComponentState.COMPONENT_STATE_PRESSED)) {
                    mTvCancel.setTextColor(mCancelTextColor.getColor(state) == 0 ? mTvCancel.getTextColor() : new Color(mCancelTextColor.getColor(state)));
                } else {
                    mTvCancel.setTextColor(mCancelTextColor.getColor(state) == 0 ? mTvCancel.getTextColor() : new Color(mCancelTextColor.getColor(ComponentState.COMPONENT_STATE_EMPTY)));
                }
            });

            boolean single = mCancelMarginTop > 0 || TextUtils.isEmpty(mTitleStr) && mViewItem == null || mBottomDrawable == null;
            setViewBackground(mTvCancel, (single ? mStateDrawableSingle : mStateDrawableBottom));


            mTvCancel.setClickedListener(component -> {
//                    mDialog.hide();
                if (mDialog.isUseAnimation) {
                    AnimationUtils.getInstance(mContext).hideDialogAnimation(mDialog);
                }else{
                    mDialog.hide();
                }
            });
            setTextViewLine(mTvCancel);
        }


        /**
         * 创建Item Drawable
         */
        private void createDrawable() {
//            mStateDrawableTop = new StateListDrawable();
            mStateDrawableTop = new StateElement();
            mStateDrawableTop.addState(new int[] {mStatePressed}, mTopPressedDrawable);
            mStateDrawableTop.addState(new int[] {BasisDialog.COMPONENT_STATE_LONG_PRESSED}, mTopPressedDrawable);
            mStateDrawableTop.addState(new int[] {mStateEmpty}, mTopDrawable);

//            mStateDrawableCenter = new StateListDrawable();
            mStateDrawableCenter = new StateElement();
            mStateDrawableCenter.addState(new int[] {mStatePressed}, mCenterPressedDrawable);
            mStateDrawableCenter.addState(new int[] {BasisDialog.COMPONENT_STATE_LONG_PRESSED}, mCenterPressedDrawable);
            mStateDrawableCenter.addState(new int[] {mStateEmpty}, mCenterDrawable);

//            mStateDrawableBottom = new StateListDrawable();
            mStateDrawableBottom = new StateElement();
            mStateDrawableBottom.addState(new int[] {mStatePressed}, mBottomPressedDrawable);
            mStateDrawableBottom.addState(new int[] {BasisDialog.COMPONENT_STATE_LONG_PRESSED}, mBottomPressedDrawable);
            mStateDrawableBottom.addState(new int[] {mStateEmpty}, mBottomDrawable);

//            mStateDrawableSingle = new StateListDrawable();
            mStateDrawableSingle = new StateElement();
            mStateDrawableSingle.addState(new int[] {mStatePressed}, mSinglePressedDrawable);
            mStateDrawableSingle.addState(new int[] {BasisDialog.COMPONENT_STATE_LONG_PRESSED}, mSinglePressedDrawable);
            mStateDrawableSingle.addState(new int[] {mStateEmpty}, mSingleDrawable);

        }

        protected ColorStateListBean getTextColor(int position, ColorStateListBean colorStateList) {
            ColorStateListBean result = colorStateList;
            if (mMapItemColor != null && mMapItemColor.containsKey(position)) {
                result = mMapItemColor.get(position);
            }
            result = result != null ? result : mItemsTextColor;
            return result;
        }

        protected static class ViewHolder {
            Image imageView;
            AlphaTextView textView;
            Component divider;
            DirectionalLayout item;


        }

        protected abstract class SheetAdapter extends BaseItemProvider {
            @Override
            public int getCount() {
                return mListItem == null ? 0 : mListItem.size();
            }

            @Override
            public SheetItem getItem(int position) {
                return mListItem == null ? null : mListItem.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            protected void setTextView(final Text textView, SheetItem data, final int position) {
                if (textView == null) {
                    return;
                }
//                holder.textView.setCompoundDrawablePadding(mTextDrawablePadding);
                textView.setAroundElementsPadding(mTextDrawablePadding);
                setTextAttribute(textView, data.text.toString(),
                        getTextColor(position, data.textColor), (int) mItemsTextSize, mItemsGravity, false);
                setTextViewLine(textView);
            }
        }
    }


}
